<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    public function searchOcba($filter)
    {
        $type = JRequest::getVar('type');
        $subsql = "";
        if ($type == "center") {
            $subsql = "AND LEFT(OCBA_CODE,1) = '0'";
        } elseif ($type == "club") {
            $subsql = "AND LEFT(OCBA_CODE,1) = '1'";
        }
        $sql = "SELECT OCBA_CODE, OCBA_NAME, OCBA_KANA FROM ocba 
                WHERE OCBA_FLAG = 1 AND (OCBA_NAME LIKE '%$filter%'
                    OR OCBA_KANA LIKE '%$filter%' OR OCBA_CODE = '$filter')
                 ";
        $this->_db->setQuery($sql.$subsql);

        return $this->_db->loadObjectList();
    }
    
    public function list_ocba_not_input($filter){
        $result = array();
       
       if($filter['cc_type'] == 'center') {
            $this->_db->setQuery("SELECT OCBA_CODE, OCBA_NAME FROM ocba WHERE LEFT(OCBA_CODE,1) = '0'");
            $ocba_list =  $this->_db->loadObjectList();
            
            foreach ($ocba_list as $ocba){
                $sql = "SELECT COUNT(*) as c_count FROM usce WHERE USCE_OCBA_CODE = '$ocba->OCBA_CODE' AND USCE_DATE = '$filter[date]'";
                $this->_db->setQuery($sql);
                if($this->_db->loadObject()->c_count == 0){
                    array_push($result,$ocba);
                }
            }
       } else {
            $this->_db->setQuery("SELECT OCBA_CODE, OCBA_NAME FROM ocba WHERE LEFT(OCBA_CODE,1) = '1'");
            $ocba_list =  $this->_db->loadObjectList();
            
            foreach ($ocba_list as $ocba){
                $sql = "SELECT COUNT(*) as c_count FROM uscl WHERE USCL_OCBA_CODE = '$ocba->OCBA_CODE' AND USCL_DATE = '$filter[date]'";
                $this->_db->setQuery($sql);
                if($this->_db->loadObject()->c_count == 0){
                    array_push($result,$ocba);
                }
            }
       }
        
        return $result;
    }
}
